﻿@page "/total"
@inject IBiliVideoService BiliVideoService

<style>
	.chart-card{
		margin:8px;
		padding:8px;
	}
</style>

<PageTitle>分区整体比较</PageTitle>
<Layout>
	<Header>
	<Title Style="color:white">分区整体比较</Title>
	</Header>
	<Layout>
		<GridRow Gutter="16">
			<GridCol Span="9">
				<Card Class="chart-card" Title="各分区操作比例 (操作/播放量)">
					<CardTabs>
						<Tabs DefaultActiveKey="1">
							<TabPane Key="1">
								<TabTemplate>点赞数</TabTemplate>
								<ChildContent>
									<ColumnChart height="200" width="200" columnData="(object[])LikeRatio.ToArray()" />
								</ChildContent>
							</TabPane>
							<TabPane Key="2">
								<TabTemplate>投币数</TabTemplate>
								<ChildContent>
									<ColumnChart height="200" width="200" columnData="(object[])CoinRatio.ToArray()" />
								</ChildContent>
							</TabPane>
							<TabPane Key="3">
								<TabTemplate>收藏数</TabTemplate>
								<ChildContent>
									<ColumnChart height="200" width="200" columnData="(object[])FavoriteRatio.ToArray()" />
								</ChildContent>
							</TabPane>
						</Tabs>
					</CardTabs>
				</Card>
				
				<Card Style="margin:8px;" Title="分区具体数据">
					@if (DanmakuRatio.Count > 0)
					{
						<Table Style="height: 200px; width:200px; " PageSize="3" TItem="TidValue"
						   DataSource="@DanmakuRatio" Size=@TableSize.Middle>
							<PropertyColumn Title="分区" Property="c=>c.TidName.Substring(0,2)" />
							<PropertyColumn Title="弹幕" Sortable Property="c=>DoubleStr(c.Value)" />
							<PropertyColumn Title="回复" Sortable Property="c=>DoubleStr(ReplyRatio.Find(i=>i.TidName==c.TidName).Value)" />
							<PropertyColumn Title="收藏" Sortable Property="c=>DoubleStr(FavoriteRatio.Find(i=>i.TidName==c.TidName).Value)" />
							<PropertyColumn Title="投币" Sortable Property="c=>DoubleStr(CoinRatio.Find(i=>i.TidName==c.TidName).Value)" />
							<PropertyColumn Title="分享" Sortable Property="c=>DoubleStr(ShareRatio.Find(i=>i.TidName==c.TidName).Value)" />
							<PropertyColumn Title="点赞" Sortable Property="c=>DoubleStr(LikeRatio.Find(i=>i.TidName==c.TidName).Value)" />
						</Table>
					}
					else
					{
						<Loading></Loading>
					}
				</Card>
					
			</GridCol>

			<GridCol Span="6">
				<Card Class="chart-card" Title="播放量(万)">
					<PieChart height="200" width="200" pieData="_pieObjectData"></PieChart>
				</Card>
				<Card Class="chart-card" Title="结论">
					总体上，bilibili热门视频中，用户对<Text Mark>音乐</Text>区的点赞、收藏等操作在观看次数中的综合比例最高；
					<br />
					<Text Mark>游戏</Text>区的视频更容易收到点赞，且播放量最多。
					<br />
					<Text Mark>科技</Text>区的视频用户发弹幕的比例最高。
					<br />
					<Text Mark>知识</Text>区的视频用户发评论的比例最高。
				</Card>
			</GridCol>
			<GridCol Span="9">
				<Card Class="chart-card" Title="分区特点">
					<RadarChart height="600" width="600" radarData="_radarObjectData"></RadarChart>
				</Card>				
			</GridCol>
		</GridRow>
	</Layout>
</Layout>


@code {

	private object[] _colunmObjectData=new object[6];
	private object[] _pieObjectData = new object[6];
	private List<RadarChart.RadarData> _radarObjectDataList=new();
	private object[] _radarObjectData = new object[36];
	public record class TidValue
	{
		public string TidName{ get; set; }
		public double Value { get; set; }
	}
	public List<TidValue> ViewNumber = new();
	public List<TidValue> DanmakuRatio = new();
	public List<TidValue> ReplyRatio= new();	
	public List<TidValue> FavoriteRatio= new();
	public List<TidValue> CoinRatio= new();
	public List<TidValue> ShareRatio= new();
	public List<TidValue> LikeRatio= new();


	protected override async Task OnInitializedAsync()
	{
		if (BiliVideoService.BiliVideos == null || BiliVideoService.BiliVideos.Count == 0)
		{
			await BiliVideoService.GetBiliVideos(500);
		}

		// 获取各个分区的投币/比例
		foreach (var tid in BiliVideoService.MasterTidVideos.Keys)
		{
			double viewNumber = VideoHelper.SumVideoView(BiliVideoService.MasterTidVideos[tid]);
			double danmakuRatio = VideoHelper.GetVideoRatio(BiliVideoService.MasterTidVideos[tid], VideoHelper.ValueType.Danmaku);
			double replyRatio = VideoHelper.GetVideoRatio(BiliVideoService.MasterTidVideos[tid], VideoHelper.ValueType.Reply);
			double favoriteRatio = VideoHelper.GetVideoRatio(BiliVideoService.MasterTidVideos[tid], VideoHelper.ValueType.Favorite);
			double coinRatio = VideoHelper.GetVideoRatio(BiliVideoService.MasterTidVideos[tid], VideoHelper.ValueType.Coin);
			double shareRatio = VideoHelper.GetVideoRatio(BiliVideoService.MasterTidVideos[tid], VideoHelper.ValueType.Share);
			double likeRatio = VideoHelper.GetVideoRatio(BiliVideoService.MasterTidVideos[tid], VideoHelper.ValueType.Like);

			if (TidHelper.TryTidToName(tid, out string tidName))
			{
				ViewNumber.Add(new() { TidName = tidName, Value = viewNumber});
				DanmakuRatio.Add(new() { TidName = tidName, Value = danmakuRatio });
				ReplyRatio.Add(new() { TidName = tidName, Value = replyRatio });
				FavoriteRatio.Add(new() { TidName = tidName, Value = favoriteRatio });
				CoinRatio.Add(new() { TidName = tidName, Value = coinRatio });
				ShareRatio.Add(new() { TidName = tidName, Value = shareRatio });
				LikeRatio.Add(new() { TidName = tidName, Value = likeRatio });

				_radarObjectDataList.Add(new() { type = "弹幕*5", value = danmakuRatio*5, item = tidName });
				_radarObjectDataList.Add(new() { type = "回复*5", value = replyRatio*5, item = tidName });
				_radarObjectDataList.Add(new() { type = "收藏", value = favoriteRatio, item = tidName });
				_radarObjectDataList.Add(new() { type = "硬币", value = coinRatio, item = tidName });
				_radarObjectDataList.Add(new() { type = "分享*4", value = shareRatio*4, item = tidName });
				_radarObjectDataList.Add(new() { type = "点赞/3", value = likeRatio/3, item = tidName });

			}			
		}
		StateHasChanged();

		for (int i = 0; i < 36; i++)
		{
			_radarObjectData[i] = new
			{
				type = _radarObjectDataList[i].type,
				item = _radarObjectDataList[i].item,
				value = _radarObjectDataList[i].value
			};
		}

		for (int i = 0; i <6; i++)
		{
			_pieObjectData[i] = new
			{
				type = ViewNumber[i].TidName.Substring(0,2),
				value = (int)ViewNumber[i].Value/10000,
			};
		}
		_colunmObjectData =(object[])LikeRatio.ToArray();
	

	}


	private string DoubleStr(double value)
	{
		return value.ToString("#.###");
	}

}